package com.growthsee.rider.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.core.page.PageDomain;
import com.growthsee.common.core.page.TableDataInfo;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.common.utils.bean.BeanUtils;
import com.growthsee.rider.domain.RiderQuestion;
import com.growthsee.rider.domain.RiderQuestionExtend;
import com.growthsee.rider.domain.RiderQuestionOption;
import com.growthsee.rider.domain.dto.RiderQuestionDto;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.param.QuestionParam;
import com.growthsee.rider.service.RiderQuestionExtendService;
import com.growthsee.rider.service.RiderQuestionOptionService;
import com.growthsee.rider.service.RiderQuestionService;
import com.growthsee.rider.utils.BeanUtilsExt;
import com.growthsee.rider.wrapper.QuestionWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csy
 * @since 2021-05-13
 */
@AllArgsConstructor
@RestController
@RequestMapping("/rider/question")
@Api("试题管理")
public class RiderQuestionController extends BaseController {

    private final RiderQuestionExtendService questionExtendService;
    private final RiderQuestionService questionService;
    private final RiderQuestionOptionService optionService;

    @ApiOperation(value = "条件查询试题", notes = "条件查询试题")
    @GetMapping
    public TableDataInfo query(PageDomain page, QuestionParam questionParam) {
        LambdaQueryWrapper<RiderQuestion> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderQuestion::getDeleted, YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(questionParam.getCourseId())){
            queryWrapper.eq(RiderQuestion::getCourseId, questionParam.getCourseId());
        }
        Page page1 = questionService.page(new Page<>(page.getPageNum(),page.getPageSize()), queryWrapper);
        val dtoPage = BeanUtilsExt.copyPageData(page1, RiderQuestionDto.class);
        QuestionWrapper.builder().propertiesHandler(dtoPage.getRecords());
        return getDataTable(dtoPage.getRecords(),dtoPage.getTotal());
    }

    @ApiOperation(value = "添加试题", notes = "添加试题")
    @PostMapping
    public AjaxResult save(@RequestBody RiderQuestionDto riderQuestionDto) {
        riderQuestionDto.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        riderQuestionDto.setCreateTime(new Date());
        questionService.save(riderQuestionDto);

        RiderQuestionExtend extend = riderQuestionDto.getExtend();
        extend.setId(riderQuestionDto.getId());
        questionExtendService.save(extend);

        if(CollectionUtils.isNotEmpty(riderQuestionDto.getOptions())){
            riderQuestionDto.getOptions().forEach(v->{
                v.setQuestionId(riderQuestionDto.getId());
            });
        }
        optionService.saveBatch(riderQuestionDto.getOptions());

        return AjaxResult.success();
    }

    @ApiOperation(value = "添加试题", notes = "添加试题")
    @DeleteMapping("{id}")
    public AjaxResult deleteById(@PathVariable String id) {

        questionService.update( Wrappers.<RiderQuestion>lambdaUpdate()
                .set(RiderQuestion::getDeleted, YesNoEnum.YES.getValue())
                .eq(RiderQuestion::getId, id)
        );

        return AjaxResult.success();
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询题型", notes = "根据id查询题型")
    public AjaxResult findOne(@PathVariable String id) {
        RiderQuestion question = questionService.getById(id);
        RiderQuestionDto questionDto = new RiderQuestionDto();
        BeanUtils.copyBeanProp(questionDto,question);
        QuestionWrapper.builder().propertiesHandler(Arrays.asList(questionDto));
        return AjaxResult.success(questionDto);
    }

    @ApiOperation(value = "添加试题", notes = "添加试题")
    @PutMapping
    public AjaxResult update(@RequestBody RiderQuestionDto riderQuestionDto) {
        riderQuestionDto.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        riderQuestionDto.setUpdateTime(new Date());
        questionService.updateById(riderQuestionDto);

        RiderQuestionExtend extend = riderQuestionDto.getExtend();
        extend.setId(riderQuestionDto.getId());
        questionExtendService.updateById(extend);
        optionService.remove(Wrappers.<RiderQuestionOption>lambdaQuery().eq(RiderQuestionOption::getQuestionId,riderQuestionDto.getId()));
        if(CollectionUtils.isNotEmpty(riderQuestionDto.getOptions())){
            riderQuestionDto.getOptions().forEach(v->{
                v.setQuestionId(riderQuestionDto.getId());
            });
        }
        optionService.saveBatch(riderQuestionDto.getOptions());
        return AjaxResult.success();
    }

}

